home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: HELP NEEDED: What the hell is wrong with my program?
- Date: Sun, 14 Jan 96 00:35:04 GMT
- Organization: none
- Message-ID: <821579704snz@genesis.demon.co.uk>
- References: <4d1qmp$h43@carbon.cudenver.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4d1qmp$h43@carbon.cudenver.edu>
- exli@ouray.cudenver.edu "ELLIE XIAO-YU LI" writes:
-
- >
- >first of all, i have to thank all of you who have answered me through email
- >or have followed up my post. and here is more details about my problem.
- >
- >the system i am using is a dec osf/1. the code is something like:
- >
- >#include <string.h>
- >
- >typedef struct {
- > .
- > .
- > .
- > char *name;
- > .
- > .
- > .
- >} aStruct;
- >
- >aStruct *ptr;
- >char *str1="this is a test";
- >char *str2="this is another test";
- >
- >ptr=(aStruct *)malloc(sizeof(aStruct));
-
- Did you include stdlib.h or otherwise declare malloc()? If not this statement
- results in undefined behaviour. Unfortunately you cast the result of malloc#
- to a pointer, it is better not to do this when writing in ANSI C since the
- compiler can then warn you there is a problem.
-
- Always test the return value of malloc agaunst NULL for failure.
-
- >ptr->name=(char *)malloc(strlen(str1));
- ^^^^^^^^^^^
- strlen(str1)+1
-
- >strcpy(ptr->name, str1); /*no problem here*/
- >...
- >/*then later i want ptr->name to point to another string*/
- >free(ptr->name);
- >ptr->name=(char *)malloc(strlen(str2));
- ^^^^^^^^^^^
- strlen(str2)+1
- >strcpy(ptr->name, str2); /*problem arised here*/
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-